<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/25 14:00:21 $)
// $RCSfile: pda_manage.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("tickets_auditlogs");
require_once ("./modules/tickets/functions_ticketsui.php");
require_once ("./modules/tickets/functions_pdaui.php");
require_once ("./modules/tickets/functions_ticketmain.php");
require_once ("./modules/tickets/functions_search.php");

/**
* ###############################################
* MANAGE TICKETS
* ###############################################
*/
if ($eventaction == "manage")
{
	unset($_ticketsearchid); unset($noresult);

	if (!empty($_POST["filtertickets"]))
	{
		$gtype = substr($_POST["filtertickets"], 0, 2);
		$gdata = substr($_POST["filtertickets"], 2);
		if ($gtype == "d_")
		{
			$_REQUEST["departmentid"] = intval($gdata);
		} else if ($gtype == "t_") {
			$explodedata = explode(":", $gdata);
			if (count($explodedata) == 2)
			{
				$_REQUEST["departmentid"] = intval($explodedata[0]);
				$_REQUEST["ticketstatusid"] = intval($explodedata[1]);
			}
		}
		
	}

	// Check to see if mass action was carried out
	if (count($_POST["itemid"]))
	{
		require_once ("./modules/tickets/functions_ticketcore.php");
		$domassaction = false;
		if ($_POST["ma_staffid"] != "")
		{
			$_ma_staffid = intval($_POST["ma_staffid"]);
			$domassaction = true;
		} else {
			$_ma_staffid = false;
		}
		if (!empty($_POST["ma_departmentid"]))
		{
			$_ma_departmentid = intval($_POST["ma_departmentid"]);
			$domassaction = true;
		} else {
			$_ma_departmentid = false;
		}
		if (!empty($_POST["ma_ticketstatusid"]))
		{
			$_ma_ticketstatusid = intval($_POST["ma_ticketstatusid"]);
			$domassaction = true;
		} else {
			$_ma_ticketstatusid = false;
		}
		if (!empty($_POST["ma_priorityid"]))
		{
			$_ma_priorityid = intval($_POST["ma_priorityid"]);
			$domassaction = true;
		} else {
			$_ma_priorityid = false;
		}
		if (trim($_POST["ma_flagtype"]) != "")
		{
			$_ma_flagtype = intval($_POST["ma_flagtype"]);
			$domassaction = true;
		} else {
			$_ma_flagtype = "-1";
		}
		if ($domassaction == true)
		{
			updateTicketStatus($_POST["itemid"], true, $_ma_staffid, $_ma_departmentid, $_ma_ticketstatusid, $_ma_priorityid, $_ma_flagtype);
		}

		if ($_POST["ma_options"] == "delete" && $_SWIFT["staff"]["tdeleteticket"] != "0")
		{
			deleteTickets($_POST["itemid"], true);
		} else if ($_POST["ma_options"] == "bandelete" && $_SWIFT["staff"]["tbandelete"] != "0") {
			banTickets($_POST["itemid"], true);
			deleteTickets($_POST["itemid"], true);
		} else if ($_POST["ma_options"] == "markasdue") {
			ticketsMarkAsDue($_POST["itemid"]);
		} else if ($_POST["ma_options"] == "markduein24") {
			ticketsMarkAsDue($_POST["itemid"], (DATENOW+86400));
		} else if ($_POST["ma_options"] == "mergetickets" && count($_POST["itemid"]) > 1) {
			// Ok, we need to merge the tickets
			require_once ("./modules/tickets/functions_ticketmain.php");

			$_ticketlist = $_POST["itemid"];
			unset($_ticketlist[0]);
			$parentticketid = $_POST["itemid"][0];
			mergeTickets($parentticketid, $_ticketlist);
		}

		header("location: index.php?_m=tickets&_a=manage".iif(!empty($_REQUEST["departmentid"]), "&departmentid=". intval($_REQUEST["departmentid"])) .iif(!empty($_REQUEST["ticketstatusid"]), "&ticketstatusid=". intval($_REQUEST["ticketstatusid"])) .iif(!empty($_REQUEST["ticketsearchid"]), "&ticketsearchid=".intval($_REQUEST["ticketsearchid"]))."&maconfirm=1");
		exit;
	} else if (!empty($_POST["s_query"])) {
		// We have a search to do...
		require_once ("./modules/tickets/functions_ticketsearch.php");

		$noresult = "";
		if ($_POST["searchfield"] == "ticketid")
		{
			$_result = searchTicketID($_POST["s_query"]);
			if ($_result["type"] == SEARCH_TICKETID)
			{
				header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_result["ticketid"]));
				exit;
			} else if ($_result["type"] == SEARCH_TICKETIDLIST) {
				$_ticketsearchid = $_result["ticketsearchid"];
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorticketid"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "fullname") {
			$_result = searchFullName($_POST["s_query"]);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorfullname"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "lastreplier") {
			$_result = searchLastReplier($_POST["s_query"]);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorlastreplier"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "contents") {
			$_result = doTicketSearch($_POST["s_query"]);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorquery"], htmlspecialchars($_POST["s_query"]));
			}
		} else if ($_POST["searchfield"] == "subject") {
			$_result = doTicketSearch($_POST["s_query"], true);
			if ($_result)
			{
				$_ticketsearchid = $_result;
			} else {
				$noresult = sprintf($_SWIFT["language"]["serrorquery"], htmlspecialchars($_POST["s_query"]));
			}
		}

		if ($_ticketsearchid)
		{
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid)."&s_query=".urlencode($_POST["s_query"])."&searchfield=".urlencode($_POST["searchfield"]));
			exit;
		}
	}

	$_assigns = getAssignedDepartments(MODULE_TICKETS);

	$_ticketselect = renderTicketSelect();

	$_ticketviewid = getDefaultTicketView($_SWIFT["staff"]["staffid"]);

	if (!empty($_ticketsearchid))
	{
		$template->assign("ticketsearchid", intval($_ticketsearchid));
	} else if (!empty($_REQUEST["ticketsearchid"])) {
		$template->assign("ticketsearchid", intval($_REQUEST["ticketsearchid"]));
		$_ticketsearchid = intval($_REQUEST["ticketsearchid"]);
	} else {
		if (!empty($_REQUEST["departmentid"]))
		{
			$template->assign("departmentid", intval($_REQUEST["departmentid"]));
		}
		if (!empty($_REQUEST["ticketstatusid"]))
		{
			$template->assign("ticketstatusid", intval($_REQUEST["ticketstatusid"]));
		}
	}

	if (!empty($_REQUEST["departmentid"]) && !empty($_REQUEST["ticketstatusid"]))
	{
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"]).'">'.$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'&ticketstatusid='. intval($_REQUEST["ticketstatusid"]) .'" title="'.addslashes($_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"]).'">'.$_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"] ." &gt; ".$_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"];
	} else if (!empty($_REQUEST["departmentid"]) && empty($_REQUEST["ticketstatusid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"]).'">'.$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"].'</a> ';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"];
	} else if (!empty($_REQUEST["ticketsearchid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&ticketsearchid='. intval($_REQUEST["ticketsearchid"]) .'" title="'.addslashes($_SWIFT["language"]["searchquery"]).'">'.$_SWIFT["language"]["searchquery"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["searchquery"];
	} else {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"];
	}

	displayPDAHeader($pagetitle, $nav);

	$_ticketstatusid = $_departmentid = array();

	if (!empty($_REQUEST["ticketstatusid"]))
	{
		$_ticketstatusid[] = intval($_REQUEST["ticketstatusid"]);
	} else {
		$_ticketstatusid = getDisplayTicketStatusList();
	}

	if (!empty($_GET["maconfirm"])) {
		$_SWIFT["infomessage"] = $_SWIFT["language"]["massactionconfirm"];
	} else if (!empty($_GET["delconfirm"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["delconfirm"], htmlspecialchars($_GET["delconfirm"]));
	} else if (!empty($_GET["banconfirm"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["banconfirm"], htmlspecialchars($_GET["banconfirm"]));
	} else if (!empty($_GET["bandconfirm"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["bandconfirm"], htmlspecialchars($_GET["bandconfirm"]));
	} else if (!empty($_GET["searchticketid"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["serrorticketid"], htmlspecialchars($_GET["searchticketid"]));
	} else if (!empty($_GET["searchquery"])) {
		$_SWIFT["infomessage"] = sprintf($_SWIFT["language"]["serrorquery"], htmlspecialchars($_GET["searchquery"]));
	}

	if (!empty($_REQUEST["departmentid"]) && in_array($_REQUEST["departmentid"], $_assigns))
	{
		$_departmentid[] = $_REQUEST["departmentid"];
	} else {
		$_departmentid = $_assigns;
	}

	if ($_SWIFT["staff"]["tmanage"] == "0")
	{
		$_SWIFT["errormessage"] = $_SWIFT["language"]["nopermission"];
	}

	$template->assign("infomessage", $_SWIFT["infomessage"]);
	$template->assign("errormessage", $_SWIFT["errormessage"]);

	if ($noresult)
	{
		$template->assign("infomessage", $noresult);
		unset($_ticketsearchid);
	}

	if ($_SWIFT["staff"]["tmanage"] != "0")
	{
		echo '<form name="pdaform" action="index.php" method="POST"><select name="filtertickets">'. $_ticketselect .'</select>&nbsp;&nbsp;<input type="submit" name="submit" value="'. $_SWIFT["language"]["filter"] .'"><BR /><input type="hidden" name="_m" value="tickets"><input type="hidden" name="_a" value="manage"></form>';

		renderPDATicketGrid($_ticketviewid, $_departmentid, $_ticketstatusid, $_ticketsearchid);
	}

	displayPDAFooter();

	$seconds = fetchmicrotime()-$_SWIFT["starttime"];











/**
* ###############################################
* VIEW TICKET
* ###############################################
*/
} else if ($eventaction == "viewticket") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"]);

	// First get the actual ticket.
	$_loadresult = $_ticketobj->loadTicketDisplay();

	$_ticketobj->lockTicket($_SWIFT["staff"]["staffid"]);
	if (!$_loadresult)
	{
		echo $_SWIFT["language"]["invalidticket"];
		exit;
	}
	$_ticket = &$_ticketobj->ticket;


	if (!empty($_REQUEST["departmentid"]) && !empty($_REQUEST["ticketstatusid"]))
	{
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"]).'">'.$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'&ticketstatusid='. intval($_REQUEST["ticketstatusid"]) .'" title="'.addslashes($_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"]).'">'.$_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"].'</a> &raquo; <a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($_ticket["ticketid"]) .'&departmentid='. intval($_REQUEST["departmentid"]) .'&ticketstatusid='. intval($_REQUEST["ticketstatusid"]) .'" title="'.addslashes($_ticket["fticketid"]).'">'.$_ticket["fticketid"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"] ." &gt; ".$_SWIFT["statuscache"][$_REQUEST["ticketstatusid"]]["title"];
	} else if (!empty($_REQUEST["departmentid"]) && empty($_REQUEST["ticketstatusid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"]).'">'.$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"].'</a> &raquo; <a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($_ticket["ticketid"]) .'&departmentid='. intval($_REQUEST["departmentid"]) .'" title="'.addslashes($_ticket["fticketid"]).'">'.$_ticket["fticketid"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"] ." &gt; ".$_SWIFT["departmentcache"][$_REQUEST["departmentid"]]["title"];
	} else if (!empty($_REQUEST["ticketsearchid"])) {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manage&ticketsearchid='. intval($_REQUEST["ticketsearchid"]) .'" title="'.addslashes($_SWIFT["language"]["searchquery"]).'">'.$_SWIFT["language"]["searchquery"].'</a> &raquo; <a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($_ticket["ticketid"]) .'&ticketsearchid='. intval($_REQUEST["ticketsearchid"]) .'" title="'.addslashes($_ticket["fticketid"]).'">'.$_ticket["fticketid"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["searchquery"];
	} else {
		$nav = '<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["manage"].'">'.$_SWIFT["language"]["manage"].'</a> &raquo; <a href="index.php?_m=tickets&_a=viewticket&ticketid='. intval($_ticket["ticketid"]) .'" title="'.addslashes($_ticket["fticketid"]).'">'.$_ticket["fticketid"].'</a>';
		$pagetitle = $_SWIFT["language"]["tickets"] ." &gt; ".$_SWIFT["language"]["manage"];
	}

	displayPDAHeader($pagetitle, $nav);

	$template->assign("infomessage", $_SWIFT["infomessage"]);
	$template->assign("errormessage", $_SWIFT["errormessage"]);

	if ($noresult)
	{
		$template->assign("infomessage", $noresult);
		unset($_ticketsearchid);
	}

	$_limit = intval($_SWIFT["settings"]["t_pdpostlimit"]);
	if (!$_limit)
	{
		$_limit = "6";
	}

	// Get the current offset for this page
	if (!empty($_REQUEST["offset"]) && is_numeric($_REQUEST["offset"]))
	{
		$_offset = intval($_REQUEST["offset"]);
	} else {
		$_offset = "0";
	}

	$totalreplies = $_ticket["totalreplies"];
	if ($_SWIFT["settings"]["t_pdenpagin"] != 1)
	{
		$_limit = false;
		$_offset = false;
		$totalreplies = 0;
	}
	$_posts = $_ticketobj->getTicketPosts($_limit, $_offset, $_SWIFT["settings"]["t_pdpostorder"]);

	$pagination = pagination("index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticket["ticketid"]), ($totalreplies+1), $_limit, $_offset, "5", "pageoftotal", TRUE, FALSE, TRUE);

	$pagination1 = '<table width="100%" border="0" cellspacing="0" cellpadding="0">'.SWIFT_CRLF;
	$pagination2 = '<tr><td><table width="100%"  border="0" cellspacing="0" cellpadding="0" id="tmenuref"><tr valign="top"><td align="left"><table border="0" cellpadding="0" cellspacing="1" class="tborder"><tr>'.$pagination.'</tr></table></td></tr>'.SWIFT_CRLF;
	$pagination3 = '<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="6" /></td></tr>'.SWIFT_CRLF;
	$pagination4 .= '</table>';

	echo $pagination1.$pagination2.'<tr><td><img src="'. $_SWIFT["themepath"] .'space.gif" width="1" height="4" /></td></tr><tr><td align="left" width="1"><table border="0" cellpadding="0" cellspacing="1" class="tborder"><tr>'. '<td class="navpageselected" nowrap><a href="index.php?_m=tickets&_a=ticketactions&action=jump&t=previous&ticketid='. intval($_ticket["ticketid"]) .'" title="'. $_SWIFT["language"]["previousticket"] .'">'. $_SWIFT["language"]["previousticket"] .'</a></td>'.'<td class="navpageselected" nowrap><a href="index.php?_m=tickets&_a=ticketactions&action=jump&t=next&ticketid='. intval($_ticket["ticketid"]) .'" title="'. $_SWIFT["language"]["nextticket"] .'">'. $_SWIFT["language"]["nextticket"] .'</a></td></tr></table></td></tr>'.$pagination3.$pagination4;
	$footerpagination = $pagination1.$pagination3.$pagination2.$pagination3.$pagination4;
	unset($pagination1); unset($pagination2); unset($pagination3); unset($pagination4);

	echo '<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tborder">'.SWIFT_CRLF;
	echo '<tbody><tr><td class="contenttableborder" colspan="'. "3" .'"><table border="0" cellpadding="0" cellspacing="1" width="100%">'.SWIFT_CRLF;

	foreach ($_ticket["notes"] as $key=>$val)
	{
		if ($val["forstaffid"] == 0 || $val["forstaffid"] == $_SWIFT["staff"]["staffid"])
		{
			if (empty($val["bystaffid"]))
			{
				$byname = $_SWIFT["language"]["alsystem"];
			} else {
				$byname = htmlspecialchars($_SWIFT["staffcache"][$val["bystaffid"]]["fullname"]);
			}
			echo '<tr><td align="left" valign="top"><table border="0" cellpadding="2" cellspacing="0" width="100%">
			<tr class="ticketnote"><td width="1" align="left"><img src="'. $_SWIFT["themepath"] .'icon_topic'. iif($val["dateline"]>$_SWIFT["staff"]["lastvisit"], "yellow") .'.gif" border="0" /></td>
			<td align="left"><span class="smalltext">'. sprintf($_SWIFT["language"]["notetitleformat"], $byname, edate($_SWIFT["settings"]["dt_datetimeformat"], $val["dateline"]), strColorDate(DATENOW-$val["dateline"], true, true)) .'</span></td><td width="1" align="right"></td></tr>
			<tr class="ticketnotecontent"><td colspan="3"><HR class="notehr"></td></tr>
			<tr class="ticketnotecontent"><td colspan="3"><span class="smalltext">'. getProcessedHTML($val["notes"]) .'</span></td></tr>
			</table></td></tr>';
		}
	}

	$rowclass = "ticketrow2";
	// Process the Posts Now.
	foreach ($_posts as $key=>$val)
	{
		if ($rowclass == "ticketrow2")
		{
			$rowclass = "ticketrow1";
		} else {
			$rowclass = "ticketrow2";
		}

		if (!empty($_ticket["phoneno"]) && $_ticket["firstpostid"] == $val["ticketpostid"])
		{
			$val["phoneticket"] = true;
		}
		?>
		<tr> 
		<td align="left" valign="top" class="<?=$rowclass?>"><table width="100%" border="0" cellspacing="0" cellpadding="4" height="100%">
		<tr> 
		<td width="50%"><span class="mediumtitle"><strong><?=htmlspecialchars($val["fullname"])?></strong></span><br><br>
		<?
		if ($val["creator"] == POST_STAFF)
		{
			?><img src="<?=$_SWIFT["themepath"]?>img_staff.gif" border="0" /><?
		} else if ($val["creator"] == POST_THIRDPARTY) {
			?><img src="<?=$_SWIFT["themepath"]?>img_thirdparty.gif" border="0" /><?
		} else if ($val["creator"] == POST_RECIPIENT) {
			?><img src="<?=$_SWIFT["themepath"]?>img_recipient.gif" border="0" /><?
		} else {
			?><img src="<?=$_SWIFT["themepath"]?>img_user.gif" border="0" /><?
		}
		?>
		</td>
		<td align="right" valign="top" width="50%" nowrap><span class="smalltext"><?
		if (!empty($val["emailto"]))
		{
			echo sprintf($_SWIFT["language"]["trtoemail"], htmlspecialchars($val["emailto"]))."<BR />";
		}
		if ($val["phoneticket"] == true)
		{
			echo htmlspecialchars($_ticket["phoneno"])."<BR />";
		}
		if (!empty($val["email"]))
		{
			echo htmlspecialchars($val["email"]);
		}
		?></span></td>
		</tr></table></td>
		</tr>

		<tr>
		<td align="left" valign="top" class="<?=$rowclass?>"><table width="100%" border="0" cellspacing="0" cellpadding="3">
		<tr><td>
		<table width="100%" border="0" cellspacing="0" cellpadding="2"><tr><td width="1"><img src="<?
		if ($val["phoneticket"] == true) {
			echo $_SWIFT["themepath"]."icon_ticketphone.gif";
		} else if ($val["dateline"] > $_SWIFT["staff"]["lastvisit"]) {
			echo $_SWIFT["themepath"]."icon_email.gif";
		} else {
			echo $_SWIFT["themepath"]."icon_emailopen.gif";
		}
		?>" align="absmiddle" border="0" /></td><td align="left"><span class="smalltext"><?=edate($_SWIFT["settings"]["dt_datetimeformat"], $val["dateline"])?></span></td><td align="right" width="50"></td></tr></table>
		<hr class="tickethr" size="1">
		<span class="smalltext">
		<?
		$contents = &$val["contents"];
		echo $contents;
		if ($val["edited"] == 1)
		{
			echo '<BR /><span class="smalltext"><i>'.sprintf($_SWIFT["language"]["lastedited"], $_SWIFT["staffcache"][$val["editedbystaffid"]]["fullname"], edate($_SWIFT["settings"]["dt_datetimeformat"], $val["editeddateline"]))."</i></span><BR />";
		}
		?>
		</span>
		</table></td></tr>
		</td>
		</tr>
		<?
	}
	echo '</table></td></tr></tbody></table>'.SWIFT_CRLF;

	echo $footerpagination;

	extract(generateTicketSelectBoxes($_ticket));

	printFormStart("pdaform", "index.php", "POST", true);
	echo '<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tborder">'.SWIFT_CRLF;
	echo '<tbody><tr><td class="contenttableborder" colspan="'. "3" .'"><table border="0" cellpadding="2" cellspacing="1" width="100%">'.SWIFT_CRLF;

	echo '<tr class="'. getRowBG() .'">
	<td align="left"><span class="tabletitle">'. $_SWIFT["language"]["qowner"] .'</span></td>
	<td align="left">'. $_poststaffselect .'&nbsp;&nbsp;</td>
	</tr>
	<tr class="'. getRowBG() .'">
	<td align="left"><span class="tabletitle">'. $_SWIFT["language"]["qdepartment"] .'</span></td>
	<td align="left">'. $_departmentselect .'</td>
	</tr>
	<tr class="'. getRowBG() .'">
	<td align="left"><span class="tabletitle">'. $_SWIFT["language"]["qstatus"] .'</span></td>
	<td align="left">'. $_poststatusselect.'</td>
	</tr>
	<tr class="'. getRowBG() .'">
	<td align="left"><span class="tabletitle">'. $_SWIFT["language"]["qtype"] .'</span></td>
	<td align="left"><span class="smalltext"><select name="replytype"><option value="reply" selected>'. $_SWIFT["language"]["pdareply"] .'</option><option value="ticketnote">'. $_SWIFT["language"]["pdaticketnote"] .'</option><option value="usernote">'. $_SWIFT["language"]["pdausernote"] .'</option></select></span></td>
	</tr>
	<tr class="'. getRowBG() .'">
	<td align="left" colspan="2"><textarea name="replycontents" style="WIDTH: 100%;" rows="6" cols="30"></textarea></td>
	</tr>
	';

	$data[0]["value"] = '<input type="submit" name="submit" value="'. $_SWIFT["language"]["submit"] .'" />'.SWIFT_CRLF;
	$data[0]["align"] = "center";
	$data[0]["colspan"] = 2;
	printDataRow($data);

	echo '</table></td></tr></tbody></table><input type="hidden" name="_m" value="tickets"><input type="hidden" name="_a" value="ticketactions"><input type="hidden" name="action" value="reply"><input type="hidden" name="departmentid" value="'. intval($_REQUEST["departmentid"]) .'"><input type="hidden" name="ticketstatusid" value="'. intval($_REQUEST["ticketstatusid"]) .'"><input type="hidden" name="ticketsearchid" value="'. intval($_REQUEST["ticketsearchid"]) .'"><input type="hidden" name="ticketid" value="'. intval($_ticket["ticketid"]) .'"></form>'.SWIFT_CRLF;


	$template->assign("backurl", "index.php?_m=tickets&_a=manage" .iif(!empty($_REQUEST["departmentid"]), "&departmentid=". intval($_REQUEST["departmentid"])) .iif(!empty($_REQUEST["ticketstatusid"]), "&ticketstatusid=". intval($_REQUEST["ticketstatusid"])) .iif(!empty($_REQUEST["ticketsearchid"]), "&ticketsearchid=".intval($_REQUEST["ticketsearchid"])));

	displayPDAFooter();






/**
* ###############################################
* TICKET ACTIONS
* ###############################################
*/
} else if ($eventaction == "ticketactions") {
	$_ticketobj =& new TicketMain($_REQUEST["ticketid"], true);
	if (!$_ticketobj->ticket)
	{
		echo $_SWIFT["language"]["errorticketaccess"];
		exit;
	}

	if ($_POST["action"] == "reply")
	{
		$_tdepartmentid = $_ticketobj->ticket["departmentid"];
		$_tticketstatusid = $_ticketobj->ticket["ticketstatusid"];

		$_gridset = &$_SWIFT["ticketgridcache"][$_SWIFT["staff"]["staffid"]];

		if ($_gridset["replynav"] ==2)
		{
			$nextticketid = $_ticketobj->getNextTicketID();
		} else {
			unset($nextticketid);
		}

		$_ticket = &$_ticketobj->ticket;

		$ticketlistmove = false;
		if ($_POST["tt_staffid"] != $_ticketobj->ticket["ownerstaffid"])
		{
			$_ticketobj->assignTicket($_POST["tt_staffid"]);
		}
		if ($_POST["tt_ticketstatusid"] != $_ticketobj->ticket["ticketstatusid"])
		{
			$_ticketobj->changeStatus($_POST["tt_ticketstatusid"]);
		}
		if ($_POST["tt_departmentid"] != $_ticketobj->ticket["departmentid"])
		{
			// Get the assigned departments
			$_ticketobj->moveTicket($_POST["tt_departmentid"]);
			$_assigns = getAssignedDepartments(MODULE_TICKETS);
			if (!in_array($_POST["tt_departmentid"], $_assigns))
			{
				// Move to Ticket List
				$ticketlistmove = true;
			}
		}

		// Do we need to add a note to this ticket?
		if (trim($_POST["replytype"]) == "ticketnote")
		{
			$_ticketobj->addTicketNote(NOTE_TICKET, "0", $_POST["replycontents"]);
		} else if (trim($_POST["replytype"]) == "usernote") {
			$_ticketobj->addTicketNote(NOTE_USER, "0", $_POST["replycontents"]);
		} else if ($_POST["replytype"] == "reply") {
			$ticketpostid = $_ticketobj->createStaffReply($_SWIFT["staff"]["staffid"], $_SWIFT["staff"]["fullname"], $_SWIFT["staff"]["email"], "", $_POST["replycontents"], false, $_ticket["emailqueueid"], "", "", false, "", false, array());
		}

		if (!$ticketlistmove && $_gridset["replynav"] == 1)
		{
			// Take back to ticket
			header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketobj->ticket["ticketid"]));
			exit;
		} else if ($nextticketid) {
			// Take to next active ticket in department
			header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($nextticketid));
			exit;
		}

		header("location: index.php?_m=tickets&_a=manage&departmentid=".intval($_tdepartmentid)."&ticketstatusid=".intval($_tticketstatusid));
		exit;
	} else if ($_GET["action"] == "jump") {
		unset($_ticketid);
		if ($_GET["t"] == "previous")
		{
			$_ticketid = $_ticketobj->getPreviousTicketID();
		} else if ($_GET["t"] == "next") {
			$_ticketid = $_ticketobj->getNextTicketID();
		}

		// Couple of things, 1) If we have the ticket id then great, just redirect to it 2) If we have no ticket id, we need to redirect to ticket listing 3) Blah Blah Blah
		if (!empty($_ticketid))
		{
			header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_ticketid));
		} else {
			header("location: index.php?_m=tickets&_a=manage&departmentid=".intval($_ticketobj->ticket["departmentid"])."&ticketstatusid=".intval($_ticketobj->ticket["ticketstatusid"]));
		}
		exit;
	}
}


?>